package com.zl.registration.util;

import com.zl.registration.exception.BusinessException;
import com.zl.registration.function.BranchHandleFunction;
import com.zl.registration.function.PresentOrElseHandlerFunction;
import com.zl.registration.function.ThrowExceptionFunction;

/**
 * 判断工具类
 *
 * @author zoulei
 * @date 2022/01/05 15:02
 */
public class JudgeUtil {

    /**
     * 如果参数为true抛出异常
     */
    public static ThrowExceptionFunction isTure(boolean b) {
        return (errorMessage) -> {
            if (b) {
                throw new BusinessException(errorMessage);
            }
        };
    }

    /**
     * 参数为true或false时，分别进行不同的操作
     */
    public static BranchHandleFunction isTureOrFalse(boolean b) {
        return (trueHandle, falseHandle) -> {
            if (b) {
                trueHandle.run();
            } else {
                falseHandle.run();
            }
        };
    }

    /**
     * 参数为空值或者非空时，分别进行不同的操作
     */
    public static PresentOrElseHandlerFunction<?> isBlankOrNoBlank(String str) {
        return (consumer, runnable) -> {
            if (str == null || str.length() == 0) {
                runnable.run();
            } else {
                consumer.accept(str);
            }
        };
    }
}
